package logic

import (
	"context"
	"month/videosvr/internal/models"
	"month/videosvr/internal/service"

	"month/videosvr/internal/svc"
	"month/videosvr/video"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadVideoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUploadVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadVideoLogic {
	return &UploadVideoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UploadVideoLogic) UploadVideo(in *video.CreateVideoReq) (*video.CreateVideoRes, error) {
	// todo: add your logic here and delete this line
	//todo 添加mysql
	tx, data, err := service.CreateVideo(models.Video{
		Userid:     int(in.Userid),
		VideoTitle: in.Title,
		SubTitle:   in.SubTitle,
		VideoUrl:   in.Video,
		ImtUrl:     in.Img,
		VideoType:  int(in.TypeId),
		Address:    in.Address,
		Status:     1,
	})

	if err != nil {
		//todo 事务回滚
		tx.Rollback()
		return &video.CreateVideoRes{}, err
	}
	//todo 添加es
	_, err = service.AddEsVideo(in.Index, data)
	if err != nil {
		//todo 事务回滚
		tx.Rollback()
		return &video.CreateVideoRes{}, err
	}
	//todo 事务提交
	tx.Commit()
	return &video.CreateVideoRes{}, nil
}
